<script>
import { useVModel } from '@vueuse/core'
export default {
  name: 'XtxTabs',
  props: {
    modelValue: {
      type: String,
      default: ''
    }
  },
  render (props, { emit }) {
    const panels = this.$slots.default()

    // console.log(panels)

    // 内容部分
    const dynamicPanels = []

    panels.forEach(item => {
      if (item.type.name === 'XtxTabsPanel') {
        dynamicPanels.push(item)
      } else {
        item.children.forEach(subitem => {
          dynamicPanels.push(subitem)
        })
      }
    })

    // 标题部分
    const nav = (
      <nav>
        {dynamicPanels.map((panel, i) => {
          return (
            <a
              class={{ active: panel.props.name === this.activeName }}
              onClick={() => this.tabClick(panel.props.name, i)}
            >
              {panel.props.label}
            </a>
          )
        })}
      </nav>
    )

    return <div class='xtx-tabs'>{[nav, dynamicPanels]}</div>
  },
  setup (props, { emit }) {
    const activeName = useVModel(props, 'modelValue', emit)

    const tabClick = (name, i) => {
      activeName.value = name

      emit('tab-click', { name, i })
    }

    return {
      activeName,
      tabClick
    }
  }
}
</script>

<style lang="less">
.xtx-tabs {
  background: #fff;
  > nav {
    height: 60px;
    line-height: 60px;
    display: flex;
    border-bottom: 1px solid #f5f5f5;
    > a {
      width: 110px;
      //   border-right: 1px solid #f5f5f5;
      text-align: center;
      font-size: 16px;
      &.active {
        border-top: 2px solid @xtxColor;
        height: 60px;
        background: #fff;
        line-height: 56px;
      }
    }
  }
}
</style>
